package com.jic.filecenter.service.upload;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class Alioss {
    @Value("${aliyun.oss.endpoint}")
    String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    String accessKeySecret;

    @Value("${aliyun.oss.bucket}")
    String bucketName;

    @Value("${aliyun.oss.facebucket}")
    String faceBucketName;

    @Value("${aliyun.oss.imgurl}")
    String imgurl;

    @Autowired
    private AliOssSDKUploadFile uploadFile;

    public String filelUpload(MultipartFile file, String type) throws IOException {
        String key = getFilename(DigestUtils.md5Hex(file.getInputStream()), type);
        Long fileSize = new Long(0);
        fileSize = file.getSize();
        String contentType = new String();
        contentType = file.getContentType();
        String originalFilename = new String();
        originalFilename = file.getOriginalFilename();
        InputStream inputStream =  file.getInputStream();

        uploadFile.aliOssUpload(key, fileSize, contentType, originalFilename, inputStream);

        return getImgURL(key);
    }


    public String upload(String key, MultipartFile file) throws IOException {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(file.getSize());
        meta.setContentType(file.getContentType());
        meta.setObjectAcl(CannedAccessControlList.PublicRead);
        String fileName = file.getOriginalFilename();
        meta.setContentDisposition("attachment;filename=\""+fileName+"\"");
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, key, file.getInputStream(), meta);
        ossClient.shutdown();
        return getImgURL(key);
    }

    public String uploadBase64(String key, String content) throws IOException {
        byte[] bytes = new BASE64Decoder().decodeBuffer(content); //将字符串转换为byte数组，这里的content是那一串base64密文 /9j/4AAQ.................，不包含（data:img/jpg;base64,）
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(inputStream.available());
        // meta.setContentType(file.getContentType());
        meta.setObjectAcl(CannedAccessControlList.PublicRead);

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, key, inputStream, meta);

        ossClient.shutdown();

        return getImgURL(key);
    }

    public String uploadFace(String key, MultipartFile file) throws IOException {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(file.getSize());
        meta.setContentType(file.getContentType());
        meta.setObjectAcl(CannedAccessControlList.PublicRead);

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(faceBucketName, key, file.getInputStream(), meta);
        ossClient.shutdown();

        return getImgURL(key);
    }

    private String getImgURL(String key) {
        return imgurl + "/" + key;
    }

    public String uploadFromInputStream(String key, InputStream inputStream) throws IOException {
        int size = 0;
        while (size == 0) {
            size = inputStream.available();
        }

        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(size);
        meta.setContentType("multipart/form-data");
        meta.setObjectAcl(CannedAccessControlList.PublicRead);

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, key, inputStream, meta);
        ossClient.shutdown();

        return getImgURL(key);
    }

    private String getFilename(String filename, String type) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        if (!StringUtils.isEmpty(type)){
            filename = type + "/" + sdf.format(dt) + "/" + filename;
        }else {
            filename = sdf.format(dt) + "/" + filename;
        }
        return filename;
    }
}
